package cn.yemuysy.code.lingting.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 敏感词模型
 */
public class SensitiveWord {
    
    /**
     * 敏感词内容
     */
    private String word;
    
    /**
     * 敏感词类型
     */
    private WordType type;
    
    /**
     * 组合敏感词的组成部分（仅组合类型时有效）
     */
    private List<String> parts;
    
    /**
     * 敏感词类型枚举
     */
    public enum WordType {
        NORMAL,      // 普通敏感词
        COMBINATION  // 组合敏感词
    }
    
    public SensitiveWord() {
        this.parts = new ArrayList<>();
    }
    
    public SensitiveWord(String word, WordType type) {
        this();
        this.word = word;
        this.type = type;
    }
    
    /**
     * 创建普通敏感词
     */
    public static SensitiveWord createNormalWord(String word) {
        return new SensitiveWord(word, WordType.NORMAL);
    }
    
    /**
     * 创建组合敏感词
     */
    public static SensitiveWord createCombinationWord(String combinationId, String... parts) {
        SensitiveWord sensitiveWord = new SensitiveWord(combinationId, WordType.COMBINATION);
        sensitiveWord.parts = Arrays.asList(parts);
        return sensitiveWord;
    }
    
    /**
     * 创建组合敏感词 - 从字符串解析
     * 支持格式：澳门+博彩+网站
     */
    public static SensitiveWord parseCombinationWord(String combinationStr) {
        String[] parts = combinationStr.split("\\+");
        return createCombinationWord(combinationStr, parts);
    }
    
    /**
     * 检查是否为组合敏感词
     */
    public boolean isCombination() {
        return type == WordType.COMBINATION;
    }
    
    /**
     * 获取组合敏感词的组成部分数量
     */
    public int getPartCount() {
        return parts.size();
    }
    
    /**
     * 检查指定单词是否为当前组合敏感词的组成部分
     */
    public boolean containsPart(String part) {
        return parts.contains(part);
    }
    
    /**
     * 获取所有未匹配的组成部分
     */
    public List<String> getUnmatchedParts(List<String> matchedParts) {
        List<String> unmatched = new ArrayList<>(parts);
        unmatched.removeAll(matchedParts);
        return unmatched;
    }
    
    /**
     * 检查组合敏感词是否完全匹配
     */
    public boolean isFullyMatched(List<String> matchedParts) {
        return parts.size() == matchedParts.size() && parts.containsAll(matchedParts);
    }
    
    // Getters and Setters
    public String getWord() {
        return word;
    }
    
    public void setWord(String word) {
        this.word = word;
    }
    
    public WordType getType() {
        return type;
    }
    
    public void setType(WordType type) {
        this.type = type;
    }
    
    public List<String> getParts() {
        return parts;
    }
    
    public void setParts(List<String> parts) {
        this.parts = parts;
    }
    
    @Override
    public String toString() {
        return "SensitiveWord{" +
                "word='" + word + '\'' +
                ", type=" + type +
                ", parts=" + parts +
                '}';
    }
    
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        
        SensitiveWord that = (SensitiveWord) o;
        
        return word != null ? word.equals(that.word) : that.word == null;
    }
    
    @Override
    public int hashCode() {
        return word != null ? word.hashCode() : 0;
    }
} 